Title: SQL W3S (By-BKK) / 018 Sum
Tags: Obsidian_to_Anki

Q: What does the SQL SUM() function do?
A: It returns the total sum of a numeric column

Q: How would you find the sum of all Quantity values in OrderDetails?
A: SELECT SUM(Quantity) FROM OrderDetails

Q: True/False: SUM() can be used only with numeric columns
A: True

Q: What is the result of SUM(column_name) when used with a WHERE clause?
A: It returns the sum of values that meet the condition

Q: How do you sum the Quantity for ProductID 11 in OrderDetails?
A: SELECT SUM(Quantity) FROM OrderDetails WHERE ProductID = 11

Q: Reverse card:
SELECT SUM(Quantity) AS total FROM OrderDetails
A: How do you name the summed column "total"?

Q: How do you use SUM() with GROUP BY?
A: GROUP BY lets you get the sum for each category or group

Q: Write a query to return the total Quantity for each OrderID
A: SELECT OrderID, SUM(Quantity) AS [Total Quantity] FROM OrderDetails GROUP BY OrderID

Q: True/False: You can use expressions inside SUM() like SUM(Quantity * 10)
A: True

Q: What does SUM(Quantity * 10) calculate?
A: The total earnings if each product costs 10 dollars

Q: How do you sum actual amounts by joining OrderDetails and Products?
A: Use SELECT SUM(Price * Quantity) FROM OrderDetails LEFT JOIN Products ON OrderDetails.ProductID = Products.ProductID

Q: Reverse card:
Use SUM(DISTINCT column_name)
A: How do you sum only unique values in a column?

Q: Syntax for SUM() with a condition
A: SELECT SUM(column_name) FROM table_name WHERE condition

Q: True/False: SUM() ignores NULL values in a column
A: True

Q: What is the purpose of the AS keyword with SUM()? #flashcard
To give the result column a name or aliasWhat does the SQL SUM() function do?
A: It returns the total sum of a numeric column

Q: How do you calculate the sum of all values in a column?
A: Use SELECT SUM(column_name) FROM table_name

Q: True/False: SUM() can only be used with numeric columns
A: True

Q: How do you sum the Quantity of all orders in OrderDetails?
A: SELECT SUM(Quantity) FROM OrderDetails

Q: What does adding a WHERE clause to SUM() do?
A: It returns the sum for values matching the condition

Q: How do you find the total Quantity for ProductID 11?
A: SELECT SUM(Quantity) FROM OrderDetails WHERE ProductID = 11

Q: What does GROUP BY with SUM() allow?
A: It calculates the sum for each group or category

Q: Write a query to get total quantity for each OrderID
A: SELECT OrderID, SUM(Quantity) AS [Total Quantity] FROM OrderDetails GROUP BY OrderID

Q: Can you use expressions inside SUM()?
A: Yes, you can use expressions like SUM(Quantity * 10)

Q: What does SUM(Quantity * 10) compute?
A: The total earnings if each product quantity is multiplied by 10

Q: How do you sum actual amounts by joining tables?
A: Use SUM(Price * Quantity) and join OrderDetails to Products

Q: Syntax to use SUM() with a condition
A: SELECT SUM(column_name) FROM table_name WHERE condition

Q: Reverse card:
SUM() ignores NULL values in a column
A: Does SUM() include NULL values in its calculation?

Q: What is the purpose of AS in SUM()?
A: To assign a name to the result column

Q: SELECT SUM(Quantity) AS total FROM OrderDetails
A: How do you give an alias to the SUM result?